package com.deyuanyun.pic.pbf.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pbf.controller.dto.defect.DefectInfoDto;
import com.deyuanyun.pic.pbf.controller.dto.defect.TaskInstanceDto;
import com.deyuanyun.pic.pbf.domain.DefectInfo;
import com.deyuanyun.pic.pbf.domain.DefectStatistics;
import com.deyuanyun.pic.pbf.service.DefectInfoService;
import com.deyuanyun.pic.pbf.service.DefectRepairMgrService;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.deyuanyun.pic.wf.domain.TaskInstance;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 内检缺陷维修过程查询控制
 *
 * @author hlw
 * @date 2016-06-16
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/pbf/defectRepairMgr")
public class DefectRepairMgrController {

    @Resource
    private DefectInfoService defectInfoService;
    @Resource
    private DefectRepairMgrService defectRepairMgrService;


    /**
     * 跳转到 作业点维修记录
     * @param modelMap
     * @return
     */
    @RequestMapping("/repairWorkRecord.html")
    public String repairWorkRecord(ModelMap modelMap) {
        return "/pbf/repairManage/repairWorkRecord";
    }


    /**
     * 查询缺陷点的相关信息
     */
    @RequestMapping("/queryDefectInfo.json")
    @ResponseBody
    public void queryDefectInfo() {
        PageJsonBean jsonBean = new PageJsonBean();
        List<DefectInfo> defectInfoList = defectRepairMgrService.queryDefectInfoList();
        List<DefectInfoDto> defectInfoDtoList = new ArrayList<DefectInfoDto>();
        for (DefectInfo defectInfo : defectInfoList) {
            DefectInfoDto defectInfoDto = new DefectInfoDto();
            defectInfoDto.fromDomain(defectInfo);
            defectInfoDtoList.add(defectInfoDto);
        }
        jsonBean.setList(defectInfoDtoList);
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据 id查询"缺陷点“的详细信息
     *
     * @param defectId 缺陷点编号
     */
    @RequestMapping(value = "/findDefectInfo/{defectId}.json")
    @ResponseBody
    public void findDefectInfo(@PathVariable String defectId) {

        DefectInfoDto defectInfoDto = new DefectInfoDto();
        defectInfoDto.fromDomain(defectInfoService.selectByPrimaryKey(defectId));
        AjaxSupport.sendSuccessText("success", defectInfoDto);
    }

    /**
     * 根据“缺陷信息编号”查询对应的维修任务
     *
     * @param defectId 缺陷信息编号
     */
    @RequestMapping(value = "/queryDefectTask/{defectId}.json")
    @ResponseBody
    public void queryDefectTask(@PathVariable String defectId) {

        List<TaskInstance> taskInstanceList = defectRepairMgrService.queryTaskInstanceList(defectId);
        List<TaskInstanceDto> taskInstanceDtoList = new ArrayList<TaskInstanceDto>();
        for (TaskInstance taskInstance : taskInstanceList) {
            TaskInstanceDto taskInstanceDto = new TaskInstanceDto();
            taskInstanceDto.fromDomain(taskInstance);
            taskInstanceDtoList.add(taskInstanceDto);
        }
        AjaxSupport.sendSuccessText("success", taskInstanceDtoList);
    }

    /**
     * 根据“流程任务编号”查询 缺陷点维修节点
     *
     * @param taskInstanceId 流程任务编号
     */
    @RequestMapping(value = "/findRepairNode/{taskId}.json")
    @ResponseBody
    public void findRepairNode(@PathVariable("taskId") String taskInstanceId) {

        isNullEmpty(taskInstanceId, "“流程任务编号”不能为空");
        AjaxSupport.sendSuccessText("success", defectRepairMgrService.findRepairNode(taskInstanceId));
    }

    /**
     * 根据“缺陷点编号”查询 缺陷点维修节点
     *
     * @param defectId 缺陷点编号
     */
    @RequestMapping(value = "/queryRepairNode.json")
    @ResponseBody
    public void queryRepairNode(String defectId) {

        isNullEmpty(defectId, "“缺陷信息编号”不能为空");
        AjaxSupport.sendSuccessText("success", defectRepairMgrService.queryRepairNode(defectId));
    }

    @RequestMapping("/toRepairStatistics.html")
    public ModelAndView toRiskStatistics() {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/pbf/repairManage/repairStatistics");
        return mav;
    }

    /**
     * 缺陷状态统计
     *
     * @param repairTaskId 维修任务编号
     */
    @RequestMapping(value = "/defectStatistics/{repairTaskId}.json")
    @ResponseBody
    public void defectStatistics(@PathVariable String repairTaskId) {

        isNullEmpty(repairTaskId, "“维修任务编号”不能为空");
        AjaxSupport.sendSuccessText("success", defectRepairMgrService.getDefectStatistics(repairTaskId));
    }

    private void isNullEmpty(String str, String errorMsg) {
        if (StringUtils.isNullEmpty(str)) {
            AjaxSupport.sendFailText("error", errorMsg);
            return;
        }
    }

}
